<?php
declare (strict_types=1);

namespace app\controller;

use app\BaseController;
use app\model\Category;
use app\model\UserBank;
use think\db\Query;

class BankController extends BaseController
{
	#银行
	function class()
	{
		$keyword = $this->request->get('keyword');
	
		$rows = Category::where(['pid'=>0,'type'=>'bank'])->order(['weigh'=>'desc'])->when(!empty($keyword),function (Query $query)use($keyword){
			$query->whereLike('name','%'.$keyword.'%');
		})->select();
		$this->writeJson($rows);
	}
	
	/**
	 * 添加银行卡
	 * @return void
	 */
	function addBank()
	{
		$name = $this->request->post('name');
		$account = $this->request->post('account');
		$bank = $this->request->post('bank');
		$bank_name = $this->request->post('bank_name');
		$mobile = $this->request->post('mobile');
		$idcard = $this->request->post('idcard');
		$bank_id = $this->request->post('bank_id/d');
		
		if (empty($name) || empty($account) || empty($bank) || empty($bank_name) || empty($mobile) || empty($idcard)||empty($bank_id)) {
			$this->writeJson(0, '参数错误');
		}
		UserBank::create([
			'bank_id' => $bank_id,
			'user_id' => $this->user_id,
			'name' => $name,
			'idcard' => $idcard,
			'account' => $account,
			'bank' => $bank,
			'bank_name' => $bank_name,
			'mobile' => $mobile,
		]);
		$this->writeJson();
	}
	
	function editBank()
	{
		$params = $this->request->post();
		$bank = UserBank::findOrFail($params['id']);
		if ($bank){
			$bank->save($params);
		}
		$this->writeJson();
	}
	
	/**
	 * 删除银行卡
	 * @return void
	 */
	function deleteBank()
	{
		$id = $this->request->post('id/d');
		$bank = UserBank::findOrFail($id);
		$bank->delete();
		$this->writeJson();
	}
	
	function detail()
	{
		$id = $this->request->get('id/d');
		$row = UserBank::findOrFail($id);
		$this->writeJson($row);
	}
	
	
	#银行卡列表
	function bankList()
	{
		$rows = UserBank::with(['class'])->where(['user_id' => $this->user_id])->order(['id' => 'desc'])->select();
		$this->writeJson($rows);
	}
}
